<!--  -->
<template>
  <div class="body">
      <div class="box">

        <el-form
    ref="ruleFormRef"
    :model="adminData"
    :rules="rules"
    label-width="auto"
    class="ruleForm"
  >
    <el-form-item label="" prop="username">
      <el-input v-model="adminData.username"  placeholder="请输入账号"  />
    </el-form-item>
    <el-form-item label="" prop="pass">
        <el-input type="password" show-password v-model="adminData.pass"  placeholder="请输入密码"  />
    </el-form-item>
    <el-form-item label="" prop="verifyCode">
        <div>
            <el-input v-model="adminData.verifyCode"  style="width:100px" placeholder="请输入验证码"  />
            <img @click="getAuthCode" style="width:100px" :src="codeData.imageCode">
        </div>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit(ruleFormRef)">登录</el-button>
    </el-form-item>
  </el-form>
      </div>
  </div>
</template>
<script lang='ts' setup>
import {authCode} from '../services/administator/auth'
import {authData,IAdmin} from '../services/administator/type'
// import {checkLogin} from '../services/administator/admin'
import {useRouter} from 'vue-router'
import { reactive,toRefs,ref,onMounted} from 'vue'
// import {convertToTree} from '../utils/treeUtils'
import {useUserStore} from '../store/index'

const userStore=useUserStore();

console.log(import.meta.env.VITE_BASE_URL);

const router=useRouter();
const codeData=ref<authData>({ codeId:"",errCode:0,imageCode:""})
const adminData=reactive<IAdmin>({
    username:"",
    pass:"",
    verifyCode:"",
     codeId:""
})
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<IAdmin>>({
    username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  pass: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  verifyCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ],
  
})
const getAuthCode=async ()=>{
    const res:any=await authCode().catch(()=>{});
    if(res?.errCode===10000){
        codeData.value=res;
        adminData.codeId=res.codeId;
    }
}

const submit=async (formEl: FormInstance | undefined)=>{

    if (!formEl) return;
    const valid=await formEl.validate().catch(()=>{});
    if(valid){


        userStore.login(adminData);

        // const res=await checkLogin(adminData).catch(()=>{});
        // console.log(1111111111,res);

        // if(res?.errCode===10000){

        //   let menus=convertToTree(res.data.menu);

        //   console.log(777,menus);
          
        //   userStore.updateToken(res.data.token);
        //   userStore.setMenus(menus);

             


        //     router.push("/");

        // }
        // else{
        //     ElMessage.error(res?.errMsg ?? "登录失败");
        //     getAuthCode();
        // }
        // // if(res?errCode===22000){
        // //     getAuthCode();
        // // }
        // console.log(111,res);
    }

    console.log(valid);
    


//   await formEl.validate(async (valid, fields) => {
//     if (valid) {
//      const res=await checkLogin(adminData);
//       console.log(111,res);

//     }  
//   })

    // alert(1);
//    const res=await checkLogin(adminData);
//    console.log(111,res);
   
   
}
onMounted(() => {
    getAuthCode();
})
</script>
<style scoped lang="less">
@import '../assets/css/login';

</style>